package com.guigu.basedservice.controller;


import com.github.pagehelper.PageInfo;
import com.guigu.basedservice.dto.BaseCarPartsSkuDto;
import com.guigu.basedservice.dto.BasePartsTypeDTO2;
import com.guigu.basedservice.dto.BasePartsTypeDto;
import com.guigu.basedservice.entity.*;
import com.guigu.basedservice.service.BaseCarPartsSkuDtoService;
import com.guigu.basedservice.service.BasePartsTypeDtoService;
import com.guigu.basedservice.service.BasePartsTypeService;
import com.guigu.basedservice.vo.BasePartsTypeVo;
import com.guigu.commonutils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 配件目录 前端控制器
 * </p>
 *
 * @author Heyuhang
 * @since 2024-09-13
 */
@CrossOrigin
@Api(description = "BasePartsType请求控制类")
@RestController
@RequestMapping("/basedservice/base-parts-type")
public class BasePartsTypeController {

    @Autowired
    private BasePartsTypeService basePartsTypeService;

    @Autowired
    private BasePartsTypeDtoService basePartsTypeDtoService;

    @Resource
    private BaseCarPartsSkuDtoService baseCarPartsSkuDtoService;

    @ApiOperation("查询所有BasePartsType分页数据")
    @RequestMapping("/show/{currentPage}/{pageSize}")
    public R show(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            @RequestBody BasePartsTypeVo vo
    ){
        System.out.println("hao"+vo);
        PageInfo<BasePartsType> basePartsTypePageInfo = basePartsTypeService.show(vo,currentPage, pageSize);
        return R.ok().data("basePartsTypePageInfo", basePartsTypePageInfo);
    }

    @ApiOperation("查询所有BasePartsType和BaseCarPartsSku分页数据")
    @RequestMapping("/getBaseCarPartsSkuDto/{currentPage}/{pageSize}")
    public PageInfo<BaseCarPartsSkuDto> getBaseCarPartsSkuDto(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize
    ){
        PageInfo<BaseCarPartsSkuDto> baseCarPartsSkuDtoPageInfo = baseCarPartsSkuDtoService.getBaseCarPartsSkuDto(currentPage,pageSize);
        System.out.println("baseCarPartsSkuDtoPageInfo++++"+baseCarPartsSkuDtoPageInfo);
        return baseCarPartsSkuDtoPageInfo;
    }

    @ApiOperation("查询所有BasePartsType和BaseCarPartsSku分页数据")
    @RequestMapping("/getBaseCarPartsSkuDto2/{currentPage}/{pageSize}")
    public PageInfo<BaseCarPartsSkuDto> getBaseCarPartsSkuDto2(
            @PathVariable Integer currentPage,
            @PathVariable Integer pageSize,
            @RequestBody BasePartsTypeVo vo
    ){
        PageInfo<BaseCarPartsSkuDto> baseCarPartsSkuDtoPageInfo = baseCarPartsSkuDtoService.getBaseCarPartsSkuDto2(currentPage,pageSize,vo);
        System.out.println("baseCarPartsSkuDtoPageInfo++++"+baseCarPartsSkuDtoPageInfo);
        return baseCarPartsSkuDtoPageInfo;
    }

//    @ApiOperation("查询所有BasePartsType的tree数据")
//    @RequestMapping("/treeBasePartsType")
//    public R treeBasePartsType(){
//        List<BasePartsType> partsTree = basePartsTypeDtoService.getPartsTree();
//        return R.ok().data("partsTree", partsTree);
//    }

    @ApiOperation("获取配件类型树结构")
    @GetMapping("/tree")
    public ResponseEntity<List<BasePartsType>> getPartsTree2() {
        List<BasePartsType> partsTree = basePartsTypeService.getTreeStructure();
        return ResponseEntity.ok(partsTree);
    }

    @ApiOperation("根据条件查询")
    @PostMapping("/queryCondition")
    public ResponseEntity<List<BasePartsType>> queryCarTypeByCondition(@RequestBody BasePartsType basePartsType) {
        String name = basePartsType.getName();
        // 根据条件查询
        List<BasePartsType> result = basePartsTypeService.findByCondition(name);
        // 返回查询结果
        return ResponseEntity.ok(result);
    }

    @ApiOperation("根据条件查询")
    @PostMapping("/queryCondition2")
    public ResponseEntity<List<BaseCarPartsSkuDto>> queryCarTypeByCondition2(@RequestBody BaseCarPartsSkuDto baseCarPartsSkuDto) {
        String name = baseCarPartsSkuDto.getName();
        // 根据条件查询
        List<BaseCarPartsSkuDto> result = basePartsTypeDtoService.findByCondition2(name);
        // 返回查询结果
        return ResponseEntity.ok(result);
    }

    @ApiOperation("添加BasePartsType数据")
    @PostMapping("/addBasePartsType")
    public R addBasePartsType(@RequestBody BasePartsType basePartsType){
        basePartsType.setVersion(BigDecimal.valueOf(0));
        basePartsTypeService.save(basePartsType);
        return R.ok().message("添加成功");
    }

    @ApiOperation("删除BasePartsType数据")
    @RequestMapping("/delete/{partTypeUuid}")
    public Boolean delete(@PathVariable String partTypeUuid){
        return basePartsTypeService.removeById(partTypeUuid);
    }

    @ApiOperation("获取单个BasePartsType数据")
    @RequestMapping("/getOne/{id}")
    public BasePartsType getOne(@PathVariable String id){
        return basePartsTypeService.getById(id);
    }

    @ApiOperation("修改BasePartsType数据")
    @RequestMapping("/update")
    public Boolean update(@RequestBody BasePartsType basePartsType){
        return basePartsTypeService.updateById(basePartsType);
    }

    @ApiOperation("启用BasePartsType数据")
    @RequestMapping("/enable")
    public Boolean enable(@RequestBody BasePartsType basePartsType){
        System.out.println("basePartsType++++"+basePartsType);
        basePartsType.setUsableFlag("Y");
        boolean b = basePartsTypeService.updateById(basePartsType);
        return b;
    }

    @ApiOperation("禁用baseCarBrand数据")
    @RequestMapping("/forbidden")
    public Boolean forbidden(@RequestBody BasePartsType basePartsType){
        basePartsType.setUsableFlag("N");
        boolean b = basePartsTypeService.updateById(basePartsType);
        return b;
    }

    @ApiOperation("查询所有BaseCarCategory分页数据")
    @RequestMapping("/list")
    public List<BasePartsType> list(){
        return basePartsTypeService.list(null);
    }



    // 获取根节点
    @GetMapping("/getRootNodes")
    public List<BasePartsType> getRootNodes() {
        List<BasePartsType> rootNodes = basePartsTypeService.getRootNodes();
        return rootNodes;
    }

    // 获取子节点
    @GetMapping("/getChildNodes")
    public List<BasePartsType> getChildNodes(@RequestParam String id) {
        List<BasePartsType> childNodes = basePartsTypeService.getChildNodes(id);
        return childNodes;
    }

    @GetMapping("/getRecursiveSql")
    public List<BasePartsType> getRecursiveSql() {
        List<BasePartsType> recursiveSql = basePartsTypeService.getRecursiveSql();
        return recursiveSql;
    }

    @RequestMapping("/add")
    public boolean add(@RequestBody BasePartsType basePartsType){
        System.out.println("basePartsType++++"+basePartsType);
        boolean save = basePartsTypeService.save(basePartsType);
        if(save){
            return true;
        }
        return false;
    }


}

